package com.uniapp.server.model;

import lombok.Data;
import org.hibernate.annotations.*;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.sql.Timestamp;
import java.util.Objects;

/**
 * @author cyt
 * @describe 最近事项
 * @create 2021-01-15 17:30
 */
@SQLDelete(sql = "update recently set deleted = 1 where id = ?")
@Where(clause = "deleted = 0")
@Entity
@Data
@DynamicInsert
@DynamicUpdate
@Table(name = "recently", schema = "uniapp", catalog = "")
public class RecentlyEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String content;
    private Integer deleted = 0;
    private boolean status;

    private Timestamp createTime;
    private Timestamp updateTime;

    @PrePersist
    public void prePersist() {
        this.createTime = this.updateTime = new Timestamp(System.currentTimeMillis());
    }

    @PreUpdate
    public void preUpdate() {
        this.updateTime = new Timestamp(System.currentTimeMillis());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        RecentlyEntity that = (RecentlyEntity) o;
        return id == that.id &&
                Objects.equals(content, that.content) &&
                Objects.equals(createTime, that.createTime) &&
                Objects.equals(updateTime, that.updateTime) &&
                Objects.equals(deleted, that.deleted);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, content, createTime, updateTime, deleted);
    }
}
